package com.dykj.fireFighting.web.rocketmq.consumer;

import com.dykj.fireFighting.api.base.appNotification.PushModel;
import com.dykj.fireFighting.api.base.push.service.PushService;
import com.dykj.fireFighting.common.core.constant.CommonConstant;
import com.dykj.fireFighting.common.core.constant.WebsocketConstant;
import com.dykj.fireFighting.common.starter.redisson.service.RedisService;
import com.dykj.fireFighting.web.base.websocket.entity.MoreMessage;
import com.dykj.fireFighting.web.model.Device;
import com.dykj.fireFighting.web.rocketmq.message.SXinPushMessage;
import com.dykj.fireFighting.web.utils.SpringUtil;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RocketMQMessageListener(
        topic = SXinPushMessage.TOPIC,
        consumerGroup = "dy-consumer-group-" + SXinPushMessage.TOPIC
)
public class SXinPushConsumer implements RocketMQListener<SXinPushMessage> {
    public static final Logger LOGGER = LoggerFactory.getLogger(SXinPushConsumer.class);
    private static final RedisService redisService = SpringUtil.getBean(RedisService.class);
    private static PushService pushService = (PushService) SpringUtil.getBean("pushService");

    @Override
    public void onMessage(SXinPushMessage sXinPushMessage) {
        Device device = sXinPushMessage.getDevice();
        String time = sXinPushMessage.getAlarmTime().substring(11);
        MoreMessage moreMessage = new MoreMessage();
        moreMessage.setMsgId(sXinPushMessage.getDevHisId());
        PushModel pushModel = new PushModel();

        pushModel.setTitle(device.getStatus());
        pushModel.setBody(device.getName() + "设备于今天" + time + "发生告警：" + device.getStatus()+ ";请及时处理");
        pushToApp(sXinPushMessage, pushModel);

        moreMessage.setMsgTitle(device.getStatus());
        moreMessage.setCmd(WebsocketConstant.CMD_WATER);
        moreMessage.setMsgTxt(device.getName() + "设备于今天" + time + "发生告警：" + device.getStatus()+ ";请及时处理");
        moreMessage.setUserIds(sXinPushMessage.getUnit().getUnitUserIds().toArray(new String[0]));
        redisService.convertAndSend("channel:moreMessage", moreMessage);
        moreMessage.setMsgTxt(device.getUnitName() + "的" + device.getName() + "设备于今天" + time + "发生告警：" + device.getStatus() + ";请及时处理");
        moreMessage.setUserIds(sXinPushMessage.getUnit().getCenterUserIds().toArray(new String[0]));
        redisService.convertAndSend("channel:moreMessage", moreMessage);

    }

    private void pushToApp(SXinPushMessage sXinPushMessage, PushModel pushModel) {
        pushModel.setAlias(sXinPushMessage.getDevice().getUnitId());
        pushModel.setClickType(CommonConstant.APPCLICKTYPE);
        boolean b = pushService.pushAlias(pushModel);
        System.out.println("===============================================================app推送结果：" + b);
    }
}
